using System;
using System.Collections.Generic;

namespace ToffeeHttp.Runtime.Core
{
    public class ToffeeHttpClient
    {
        private Dictionary<string, HttpConfig> _httpConfigs = new Dictionary<string, HttpConfig>();

        public void SetServerConfig(Dictionary<string, HttpConfig> configs)
        {
            if (configs != null)
            {
                this._httpConfigs = configs;
            }
        }

        public void AppendServer<T, T1>(string serverPath, int port, int timeOut, string serverGroup) where T : IRequestHandler, new() where T1 : IMsgChannel, new()
        {
            var handler = new T();
            var channel = new T1();
            this.AppendServer(handler , channel , serverPath , port , timeOut , serverGroup);
        }

        public void AppendServer(IRequestHandler requestHandler, IMsgChannel channel, string serverPath, int port, int timeOut, string serverGroup)
        {
            this._httpConfigs[serverGroup] = new HttpConfig()
                                             {
                                                 ServerPath     = this.HttpCheck(serverPath),
                                                 Port           = port,
                                                 TimeOut        = timeOut,
                                                 RequestHandler = requestHandler,
                                                 MsgChannel     = channel
                                             };
        }

        public void QuickHttpSend(string uri , object data , Action<HttpResponse> callback, IRequestHandler requestHandler = null, IMsgChannel msgChannel = null, int outTime = 20)
        {
            if (requestHandler == null)
                requestHandler = new GetHandlerString();
            if (msgChannel == null)
                msgChannel = new MsgChannelDefault();
            var httpRequest = new HttpRequest(requestHandler, msgChannel, new Uri(this.HttpCheck(uri)), data);
            httpRequest.webRequest.timeout = outTime;
            httpRequest.Send(callback);
        }

        public void HttpSend(string serverGroup, string method, object data, Action<HttpResponse> callback)
        {
            var request = this.CreateHttpRequest(method, data, serverGroup);
            request.Send(callback);
        }

        public HttpRequest CreateHttpRequest(string method, object data, string serverGroup)
        {
            if (this._httpConfigs.ContainsKey(serverGroup) == false)
            {
                throw new Exception("无效ServerGroup，请检查是否配置了指定服务器接口");
            }
            var server      = this._httpConfigs[serverGroup];
            Uri uri         = new Uri(server.ServerPath + ":" + server.Port + method);
            var httpRequest = new HttpRequest(server.RequestHandler, server.MsgChannel, uri, data);
            httpRequest.webRequest.timeout = server.TimeOut;
            return httpRequest;
        }

        private string HttpCheck(string url)
        {
            if (url.IndexOf("http://" , StringComparison.OrdinalIgnoreCase) == -1 && url.IndexOf("https://" , StringComparison.OrdinalIgnoreCase) == -1 )
            {
                url = url.Insert(0, "http://");
            }
            return url;
        }
    }
}